package com.wk.pay.listener;

import com.alipay.api.AlipayApiException;
import com.wk.common.constant.mq.ItemExchange;
import com.wk.common.constant.mq.ItemQueue;
import com.wk.common.constant.mq.ItemRoutingKey;
import com.wk.pay.config.AlipayTemplate;
import com.wk.pay.controller.PayController;
import com.wk.pay.vo.PayVo;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 监听订单创建完成后发送到队列中的支付对象
 */
@Component
public class PayListener {

    @Autowired
    private AlipayTemplate alipayTemplate;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = ItemExchange.ORDER_EXCHANGE,ignoreDeclarationExceptions = "true"),
            value = @Queue(value = ItemQueue.ORDER_PAY_QUEUE,durable = "true"),
            key = ItemRoutingKey.ORDER_PAY
    ))
    public void payListener(PayVo payVo) {
        try {
            String form = alipayTemplate.pay(payVo);
            //返回值字符串传递到前台，用$("#id").html(form)类似的方法将字符串追加到页面dom对象 生成支付页
            System.out.println("form = " + form);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

}
